#!/bin/bash
#description: RabbitMQ start-stop script.
# by oilive
#rabbitmq启停路径
rabbitmq_sbin=/home/rabbitmq/sbin/

#判断是否存在进程
judge(){
	num=`ps -ef |grep rabbitmq |grep -v grep |grep erlang |wc -l`
	if [ ${num} -eq 0 ];then
		return 0
	else
		return 1
	fi
}
function start(){
	if judge;then
		"${rabbitmq_sbin}rabbitmq-server -detached"
		echo -e "\033[31mrabbitmq\033[0m \033[33mnot start\033[0m, and must execute \033[32mnew commands\033[0m!!!"
		echo -e "\033[34mplease copy and executing commands\033[0m: \033[32m${rabbit_url}rabbitmq-server -detached\033[0m"
	else
		echo -e "\033[31mrabbitmq\033[0m is \033[32mrunning\033[0m, so \033[33mno\033[0m need \033[31mstart\033[0m!!!" 
	fi
}
function stop(){
	if judge;then
		echo -e "\033[31mrabbitmq\033[0m \033[33mnot start\033[0m, so \033[33mno\033[0m need \033[31mstop\033[0m!!!" 
	else
		PID=`ps -ef|grep rabbitmq|grep erlang|awk '{print $2}'`
		kill -9 $PID
		echo -e "\033[31mrabbitmq\033[0m \033[33mstop\033[0m \033[32msuccessfully\033[0m!!!"
	fi
}
function proc(){
	if judge;then
		echo -e "\033[31mrabbitmq\033[0m \033[33mnot start\033[0m, so \033[33mdoesn't\033[0m have \033[31mrabbitmq\033[0m \033[34mprocess\033[0m!!!" 
	else
		echo -e "\033[31mrabbitmq\033[0m is \033[32mrunning\033[0m, \033[34mSee next line for process content\033[0m!!!"
		ps -ef|grep rabbitmq | grep erlang
	fi
}
#查看集群状态
function status(){
	echo "$name rabbitmq集群状态："
	echo "${rabbitmq_sbin}rabbitmqctl cluster_status"
}
function usage(){
	echo "请使用: $0 [start|stop|restart|ps|status]来运行"
    exit 1
}
case "$1" in
	'start')
		start
	;;
	'stop')
		stop
	;;
	'restart')
		stop
		start
	;;
	'ps')
		proc
	;;
	'status')
		status
	;;
	*)
		usage
	;;
esac